<template>
  <div class="video" v-title :data-title="$t('router.videoTitle')">
    <div class="headNav scrollbar">
      <el-menu
        :default-active="onRoutes"
        class="el-menu-demo"
        mode="horizontal"
        text-color="#000"
        active-text-color="#A1322C"
        @select="handleSelect"
        router
      >
        <el-menu-item index="all">
          <span>{{ $t("public.all") }}</span>
        </el-menu-item>
        <template v-for="item in navData">
          <el-menu-item :index="item.categoryId + ''" :key="item.categoryId">
            <span
              v-text="
                $i18n.locale === 'zh' ? item.categoryCname : item.categoryName
              "
            ></span>
          </el-menu-item>
        </template>
      </el-menu>
    </div>
    <div
      class="list"
      v-infinite-scroll="load"
      infinite-scroll-disabled="isPage"
      infinite-scroll-delay="120"
      infinite-scroll-distance="10"
      infinite-scroll-immediate="false"
    >
      <video-list
        :videoList="videoList"
        :loading="loading"
        v-loading="fullscreenLoading"
      >
      </video-list>
    </div>
    <nice-empty
      v-show="videoList.length == 0 && fullscreenLoading == false"
      :emptyText="$t('public.noData')"
    ></nice-empty>
    <div class="page-wrap" v-show="isPage">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-size="params.pageSize"
        background
        hide-on-single-page
        layout="total, prev, pager, next, jumper"
        :total="total"
        :small="isMobile"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
// import { createVideo } from "@/model/video";
import videoList from "@/components/common/videoList/Index";
export default {
  data() {
    return {
      navData: [],
      videoList: [],
      // 是否显示加载动画
      loading: false,
      // 请求参数
      params: {
        // 返回数量
        pageSize: 30,
        // 当前页数
        pageNum: 1,
        categoryId: this.$route.params.id == "all" ? "" : this.$route.params.id,
      },
      //数据载入动画
      fullscreenLoading: true,
      //是否显示分页
      isPage: false,
      total: 0, //总条数
    };
  },
  components: {
    videoList,
  },
  computed: {
    onRoutes() {
      return this.$route.params.id;
    },
    isMobile() {
      return this.$store.getters.isMobile;
    },
    isIE() {
      return this.$store.getters.isIE;
    },
  },
  watch: {},
  created() {},
  mounted() {
    if (this.isIE) {
      this.params.pageSize = 48;
    }
    this.getVideoCategory();
  },
  methods: {
    handleSelect(val) {
      // if (val == "all") {
      //   val = "";
      // }
      this.params.pageNum = 1;
      this.params.categoryId = val === "all" ? "" : val;
      this.isPage = false;
      this.videoList = [];
      this.getVideoList();
    },
    //分页 每页条数
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.getVideoList();
    },
    //当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.videoList = [];
      this.getVideoList();
    },
    // 获取视频分类
    async getVideoCategory() {
      try {
        let res = await this.$api.video.getVideoCategory({});
        if (res.code === 1000000) {
          this.navData = res.source ? res.source : [];
        }
        this.getVideoList();
      } catch (e) {
        console.log(e);
      }
    },
    // 获取视频列表
    async getVideoList() {
      this.fullscreenLoading = true;
      try {
        let res = await this.$api.video.getVideoList(this.params);
        if (res.code === 1000000) {
          // this.videoList = this.videoList.concat(
          //   this._normalizeVideos(res.data)
          // );
          this.videoList = res.source
            ? this.utils.processingData(this.videoList, res.source)
            : [];
          this.total = res.total;
          this.fullscreenLoading = false;
          if (res.size < 48 && res.size < res.total) {
            this.loading = true;
            this.isPage = false;
          } else {
            this.loading = false;
            this.isPage = true;
          }
        } else {
          this.$message({
            showClose: true,
            message: this.$t("public.dataAcquisitionFailed"),
            type: "error",
          });
          this.fullscreenLoading = false;
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 加载更多
    load() {
      this.params.pageSize += 18;
      this.getVideoList();
    },
    // 处理视频
    // _normalizeVideos(list) {
    //   let ret = [];
    //   list.map((item) => {
    //     if (item.id) {
    //       ret.push(
    //         createVideo({
    //           id: item.id,
    //           nickName: item.artistName,
    //           name: item.name,
    //           playCount: item.playCount,
    //           duration: item.duration,
    //           image: item.cover,
    //         })
    //       );
    //     }
    //   });
    //   return ret;
    // },
  },
};
</script>

<style lang="scss" scoped>
.video {
  .headNav {
    margin-bottom: 20px;
  }
}
</style>
